package com.example.ming.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ming.R;
import com.example.ming.bean.UserBean;
import com.example.ming.utils.AnalysisUtils;
import com.example.ming.utils.DBUtiles;

public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {

    private String spUserName;
    private static final int CHANGE_NICKNAME=1;//修改昵称的自定义常量
    private static final int CHANGE_SIGNATURE=2;//修改签名的自定义常量
    private TextView tv_back;
    private TextView tv_main_title;
    private RelativeLayout rl_title_bar;
    private RelativeLayout rl_nickName;
    private RelativeLayout rl_sex;
    private RelativeLayout rl_signature;
    private TextView tv_nickName;
    private TextView tv_user_name;
    private TextView tv_sex;
    private TextView tv_signature;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        spUserName = AnalysisUtils.readLoginUserName(this);
        init();
        initData();
        setListener();
    }

    private void init() {
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("个人资料");
        rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        rl_nickName = (RelativeLayout) findViewById(R.id.rl_nickName);
        rl_sex = (RelativeLayout) findViewById(R.id.rl_sex);
        rl_signature = (RelativeLayout) findViewById(R.id.rl_signature);
        tv_nickName = (TextView) findViewById(R.id.tv_nickName);
        tv_user_name = (TextView) findViewById(R.id.tv_user_name);
        tv_sex = (TextView) findViewById(R.id.tv_sex);
        tv_signature = (TextView) findViewById(R.id.tv_signature);
    }

    private void initData() {//获取数据
        UserBean bean=null;
        bean = DBUtiles.getInstance(this).getUserInfo(spUserName);
        if (bean==null){//判断数据库是否有数据
            bean=new UserBean();
            bean.userName=spUserName;
            bean.nickName="铭";
            bean.sex="男";
            bean.signature="0";
            DBUtiles.getInstance(this).saveUserInfo(bean);//保存用户信息到数据库
        }
        setValue(bean);
    }

    private void setValue(UserBean bean) {//为界面控件设置值
        tv_nickName.setText(bean.nickName);
        tv_user_name.setText(bean.userName);
        tv_sex.setText(bean.sex);
        tv_signature.setText(bean.signature);
    }

    private void setListener() {//设置控件的点击监听事件
        tv_back.setOnClickListener(this);
        rl_nickName.setOnClickListener(this);
        rl_sex.setOnClickListener(this);
        rl_signature.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {//控件的点击事件
        switch (view.getId()){
            case R.id.tv_back://返回键
                this.finish();
                break;
            case  R.id.rl_nickName://昵称
                String name = tv_nickName.getText().toString();//获取昵称控件的数据
                Bundle bdName = new Bundle();
                bdName.putString("content",name);//传递界面上的昵称数据
                bdName.putString("title","昵称");
                bdName.putInt("flag",1);//flag是1是修改昵称
                enterActivityForResult(ChangeUserInfoActivity.class,CHANGE_NICKNAME,bdName);//转跳到个人资料修改界面
                break;
            case R.id.rl_sex://性别
                String sex=tv_sex.getText().toString();
                sexDialog(sex);
                break;
            case R.id.rl_signature://签名
                String signature = tv_signature.getText().toString();//获取签名控件的数据
                Bundle bdSignature = new Bundle();
                bdSignature.putString("content",signature);//传递界面上的签名数据
                bdSignature.putString("title","签名");
                bdSignature.putInt("flag",2);//flag是2是修改签名
                enterActivityForResult(ChangeUserInfoActivity.class,CHANGE_SIGNATURE,bdSignature);//转跳到个人资料修改界面
                break;
            default:
                break;
        }
    }

    private void sexDialog(String sex){//设置性别弹出框
        int sexFlag=0;
        if ("男".equals(sex)){
            sexFlag=0;
        }else if ("女".equals(sex)){
            sexFlag=1;
        }
        final String items[]={"男","女"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("性别");//标题
        builder.setSingleChoiceItems(items, sexFlag, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Toast.makeText(UserInfoActivity.this, items[which], Toast.LENGTH_SHORT).show();
                setSex(items[which]);
            }
        });
        builder.create().show();
    }

    private void setSex(String sex){//更新界面上的性别数据
        tv_sex.setText(sex);
        //更新数据库里的性别字段
        DBUtiles.getInstance(UserInfoActivity.this).updateUserInfo("sex",sex,spUserName);
    }

    //获取获取数据时需要使用的方法，第一个to表示需要跳转的页面
    //第二个参数requestCode表示一个请求码
    //第三个参数b表示跳转时传递的数据
    public void enterActivityForResult(Class<?> to, int requestCode, Bundle b){
        Intent i = new Intent(this, to);
        i.putExtras(b);
        startActivityForResult(i,requestCode);
    }

    //回传数据；重写onActivityResult方法
    private String new_info;//最新数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case CHANGE_NICKNAME://个人资料修改界面回传过来的昵称数据
                if (data != null){
                    new_info = data.getStringExtra("nickName");
                    if (TextUtils.isEmpty(new_info)){
                        return;
                    }
                    tv_nickName.setText(new_info);
                    //更新数据库中的昵称字段
                    DBUtiles.getInstance(UserInfoActivity.this).updateUserInfo("nickName", new_info, spUserName);
                }
                break;
            case CHANGE_SIGNATURE://个人资料修改界面回传过来的签名数据
                if (data != null){
                    new_info = data.getStringExtra("signature");
                    if (TextUtils.isEmpty(new_info)){
                        return;
                    }
                    tv_signature.setText(new_info);
                    //更新数据库中的签名字段
                    DBUtiles.getInstance(UserInfoActivity.this).updateUserInfo("signature", new_info, spUserName);
                }
                break;
        }
    }

}
